﻿(function () {
    /*
    *Author Kwan
    *Date 2010-11-22
    *Function jBase class
    */

    //check the class name , it will be replaced when existed
    if (window.jBase) {
        alert('variable jBase has been used,it will be replaced with _jBase!');
        window._jBase = window.jBase;
    }

    //constructor
    window.jBase = function (param) {
        param = param instanceof Object ? param : {};
        this.cfg = param.cfg || {};
    }
    var pt = jBase.prototype;

    pt.html = [];
    pt.html['School_info'] = "<div id=School_info_XNumber><table border=0 cellPadding=0 cellSpacing=0 class=weight780 style=word-break:break-all><tr><td class=weight125 valign=top align=right>时间：</td><td valign=top align=left>XStartTime 至 XEndTime</td><td class=weight80 rowspan=2 valign=top align=left>School_Button</td></tr><tr><td valign=top align=right>学校：</td><td valign=top>XSchoolName</td></tr><tr><td valign=top align=right>专业：</td><td colspan=2 valign=top>XSubject</td></tr></table><table border=0 cellspacing=0 cellpadding=0 class=linedot><tr><td valign=top/></tr></table></div>";
    pt.html['Work_info'] = "<div id=Work_info_XNumber><table border=0 cellPadding=0 cellSpacing=0 class=weight780 style=word-break:break-all><tr><td class=weight125 height=30 valign=top align=right>时间：</td><td valign=top align=left>XStartTime 至 XEndTime</td><td class=weight80 rowspan=2 valign=top align=left>Work_Button</td></tr><tr><td valign=top align=right>公司：</td><td valign=top>XCompany</td></tr><tr><td valign=top align=right>部门：</td><td valign=top>XDepartment</td></tr><tr><td valign=top align=right>职位：</td><td valign=top>XPosition</td></tr><tr><td valign=top align=right>工作描述：</td><td id=Cur_Val valign=top>XMemo</td></tr></table><table border=0 cellspacing=0 cellpadding=0 class=linedot><tr><td valign=top/></tr></table></div>";
    pt.html['Skill_info'] = "<div id=Skill_info_XNumber><table class=weight780 border=0 cellpadding=0 cellspacing=0><tr><td class=it_1>技能：</td><td class=it_2>XSkillName</td><td align=right class=it_3>使用时间：</td><td class=it_4>XLifeTime月</td><td align=right class=it_5>掌握程度：</td><td valign=top align=left>XMastery</td><td class=weight80 rowspan=2 valign=top align=left>Skill_Button</td></tr><tr><td class=height5/></tr></table></div>";
    pt.html['Cer_info'] = "<div id=Cer_info_XNumber><table border=0 cellspacing=0 cellpadding=0 class=weight780><tr><td class=weight125 height=30 valign=top align=right>获得时间：</td><td valign=top align=left>XGetTime</td><td class=weight80 valign=top align=left rowspan=2>Cer_Button</td></tr><tr><td valign=top align=right>证书名称：</td><td valign=top>XCerName</td></tr><tr><td valign=top align=right>成绩：</td><td valign=top>XScore</td></tr></table><table border=0 cellspacing=0 cellpadding=0 class=linedot><tr><td valign=top/></tr></table></div>";


    /********************** DOM 对象位置相关方法 end *************************/
    pt.span = function (id, num, val) {
        return "<span id=span" + id + "_" + num + " class=hide >" + val + "</span>";
    }
    pt.inputText = function (id, num, other) {
        return "<input id=txt" + id + "_" + num + " type=text " + other + " />";
    }
    pt.inputImg = function (id, num, other) {//id='e_Cer_info_07efff848408wwd48x48'
        return "<input type='image' id='e_" + id + "_info_" + num + "' src='/images/btn_amend.gif' onclick='jbs.doAll(this);return false;' class='pointer hide' /><input type='image' id='s_" + id + "_info_" + num + "' src=/images/btn_save.gif onclick='jbs.doAll(this);return false;' class='pointer'/><div id='del_" + id + "_info_" + num + "' onclick='jbs.doDelete(this)'><a href='javascript:;'>删除</a></div>";
    }
    pt.Botton = function (id, num, other) {
        return "<input name=txt" + id + "_" + num + " type='button' class='selebg2' id=txt" + id + "_" + num + " " + other + " />";
    }
    pt.select = function (id, num, other, arr) {
        var strOption = "";
        for (var i = 0; i < arr.length; i++) {
            strOption += "<option>" + arr[i] + "</option>";
        }
        return "<select id='r" + id + "_" + num + "' class='selectfield' " + other + ">" + strOption + "</select>";
    }
    pt.addinfo = function (s) {
        var num = Math.floor(Math.random() * 256);
        var str = this.html[s];
        str = str.replace(/XNumber/g, num);
        str = str.replace(/Cer_Button/g, this.inputImg('Cer', num, ""));
        str = str.replace(/XCerName/g, this.span('CerName', num, "") + this.inputText('CerName', num, ""));
        str = str.replace(/XGetTime/g, this.span('GetTime', num, "") + this.Botton('GetTime', num, ""));
        str = str.replace(/XScore/g, this.span('Score', num, "") + this.inputText('Score', num, ""));

        str = str.replace(/School_Button/g, this.inputImg('School', num, ""));
        str = str.replace(/XStartTime/g, this.span('StartTime', num, "") + this.Botton('StartTime', num, "onclick='showcalendar(event, this);'"));
        str = str.replace(/XEndTime/g, this.span('EndTime', num, "") + this.Botton('EndTime', num, "onclick='showcalendar(event, this);'"));
        str = str.replace(/XEducation/g, this.span('Education', num, "") + this.select('Education', num, "", ['无', '初中', '高中', '中技', '中专', '大专', '本科', 'MBA', '硕士', '博士']));
        str = str.replace(/XSchoolName/g, this.span('SchoolName', num, "") + this.inputText('SchoolName', num, ""));
        str = str.replace(/XSubject/g, this.span('Subject', num, "") + this.inputText('Subject', num, ""));

        str = str.replace(/Work_Button/g, this.inputImg('Work', num, ""));
        str = str.replace(/XCompany/g, this.span('Company', num, "") + this.inputText('Company', num, ""));
        str = str.replace(/XDepartment/g, this.span('Department', num, "") + this.inputText('Department', num, ""));
        str = str.replace(/XPosition/g, this.span('Position', num, "") + this.inputText('Position', num, ""));
        str = str.replace(/XMemo/g, this.span('Memo', num, "") + this.inputText('Memo', num, ""));

        str = str.replace(/Skill_Button/g, this.inputImg('Skill', num, ""));
        str = str.replace(/XSkillName/g, this.span('SkillName', num, "") + this.inputText('SkillName', num, ""));
        str = str.replace(/XLifeTime/g, this.span('LifeTime', num, "") + this.inputText('LifeTime', num, ""));
        str = str.replace(/XMastery/g, this.span('Mastery', num, "") + this.select('Mastery', num, "", ['无', '精通', '熟练', '一般', '了解']));
        $("#" + s + "_add").append(str);

    }
    pt.doShowHide = function (s, h) {
        h.each(function () { $(this).hide(); });
        s.each(function () { $(this).fadeIn(); });
    }
    pt.btnShowHide = function (arr, h) {
        arr.each(function () { if (this == h) { $(this).hide(); } else { $(this).fadeIn(); } });
    }
    pt.BPI = function (act) {
        //alert($(act).parent().parent().parent().parent().parent().attr('id')); return false;
        var arrInput = $("#BPI_info :input:not(:image)");
        var arrSpan = $("#BPI_info span");
        var btn = $("#BPI_info :image");
        var showField = act.id == 'BPIe' ? arrInput : arrSpan;
        var hideField = act.id == 'BPIe' ? arrSpan : arrInput;
        this.btnShowHide(btn, act);
        this.doShowHide(showField, hideField);
    }
    pt.doDelete = function (act) {
        if (!confirm('删除后将无法恢复，你确定要删除吗？')) return false;
        var arr = act.id.split('_');
        var hiddenID = $("#" + arr[1] + "_" + arr[2] + "_" + arr[3] + "_hidden").val();
        var elInfo = arr[3];
        var ctrl = (typeof hiddenID != "undefined");
        var delID = elInfo.length > 5 ? arr[3] : "";
        var id = (ctrl && elInfo.length < 5) ? hiddenID : delID;
        var action = arr[1] == 'BPI' ? 'Resume' : arr[1];
        if (id != "") {
            $.ajax({ url: "facade.axd", dataType: "json", data: encodeURI("action=" + action + "&s_method=delete&id=" + id), cache: false,
                beforeSend: function (x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); },
                complete: function (x) { pt.Complete(); }
            });
        }
        $("#" + arr[1] + "_" + arr[2] + "_" + arr[3]).remove();
    }
    pt.doAll = function (act) {
        var arr = act.id.split('_');
        var ctrl = arr[0] == 'e' ? true : false;
        var parentEL = arr[1] + '_' + arr[2] + '_' + arr[3]; //父元素
        var elInfo = arr[3];
        var del = "<div id='del_" + parentEL + "'><a href='javascript:;' onclick='jbs.doDelete(this)'>删除</a></div>";
        $("#del_" + parentEL).toggle();
        if ('BPI' == arr[1]) { $('#spanLogo').toggle(); }
        //alert("parentEL:" + parentEL + "\r\nelInfo:" + elInfo);
        var arrInput = $("#" + parentEL + " :input:not(:image)");
        var arrSpan = $("#" + parentEL + " span");
        var btn = $("#" + parentEL + " :image");
        var showField = ctrl ? arrInput : arrSpan;
        var hideField = ctrl ? arrSpan : arrInput;
        //alert(arrInput.length + "," + arrSpan.length);
        this.doSetVal(showField, hideField, arr[1], ctrl, elInfo);
        this.doShowHide(showField, hideField);
        this.btnShowHide(btn, act);
    }
    pt.doSetVal = function (sf, hf, parent, ctrl, elInfo) {
        var newVal = new Array();
        var saveEL = new Array();
        var txtWorkYear = ""; var txtLogo = "";
        hf.each(function (x) {
            newVal[x] = ctrl ? $(this).html() : $(this).val();
            if (this.id == 'txtWorkYear') { txtWorkYear = $(this).find("option:selected").text(); }
        });
        sf.each(function (x) {
            if (ctrl) {
                $(this).val(newVal[x]); //为文本框赋值
            } else {
                //alert($(this).attr('id') + ',' + newVal[x]);
                $(this).html(newVal[x]);
                if (this.id == 'spanWorkYear') { $(this).html(txtWorkYear); }
                saveEL[x] = $(this).attr('id').replace('span', '');
                saveEL[x] = saveEL[x].replace('_' + elInfo, '');
            }
        });
        if (!ctrl) {
            var hiddenID = $("#" + parent + "_info_" + elInfo + "_hidden").val();
            var editID = elInfo.length > 5 ? elInfo : "";
            var id = (typeof hiddenID != "undefined" && elInfo.length < 5) ? hiddenID : editID;
            var Json = this.strJson(id, saveEL, newVal);
            var action = parent == 'BPI' ? 'Resume' : parent;
            this.saveJson(action, id, Json, parent + "_info_" + elInfo);
        }

    }

    pt.strJson = function (id, el, val) {
        var strHead = id == "" ? "{" : "{\"id\":\"" + id + "\",";
        strHead += id == rid ? "\"ModifiedDate\":\"" + this.getDateStr() + "\",\"UID\":\"" + uid + "\",\"Match\":\"0\",\"Control\":\"1\",\"Logo\":\"" + $('#txtLogo').attr('src') + "\"," : "\"ResumeID\":\"" + rid + "\",";
        var strMiddle = "";
        var strBottom = "}";
        for (var i = 0; i < el.length; i++) {
            strMiddle += "\"" + el[i] + "\":\"" + val[i] + "\"";
            strMiddle += i == el.length - 1 ? "" : ",";
        }
        return strHead + strMiddle + strBottom;
    }
    pt.saveJson = function (action, id, Json, obj) {
        id = id == "" ? "" : "&id=" + id;
        $.ajax({ url: "facade.axd", dataType: "json", data: encodeURI("action=" + action + "&s_method=save" + id + "&data=" + Json), cache: false,
            beforeSend: function (x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); },
            complete: function (x) {
                var jsonStr = eval("(" + x.responseText + ")");
                var isSave = jsonStr.success;
                //alert(x.responseText);
                if (isSave) {
                    var inputStr = id == "" ? "<input id='" + obj + "_hidden' type='hidden' value='" + jsonStr.data + "' />" : "";
                    $('#' + obj).append(inputStr);
                    pt.Complete();
                }
                else { }
                //alert(x.responseText);
            }
        });
    }
    pt.popup = function popup(message) {
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        var dialogTop = 100;
        var dialogLeft = (maskWidth / 2) - ($('#dialog-box').width() / 2);
        $('#dialog-overlay').css({ height: maskHeight, width: maskWidth }).show();
        $('#dialog-box').css({ top: dialogTop, left: dialogLeft }).fadeIn();
        //$('#dialog-message').html(message);
        $('.btn-close').click(function () { $('#dialog-overlay, #dialog-box').hide(); return false; });
        $(window).resize(function () { if (!$('#dialog-box').is(':hidden')) popup(); });
    }

    pt.Complete = function () {
        var arr = ['BPI', 'School', 'Work', 'Skill', 'Cer'];
        var orareg = 0;
        for (var i = 0; i < arr.length; i++) {
            var x = arr[i] == 'BPI' ? arr[i] + '_info_' + rid : arr[i] + '_info_add';
            var hasChild = $('#' + x + ' div').length > 0 ? true : false;
            var src = hasChild ? '/images/icon_need.gif' : '/images/icon_incomplete.gif';
            $('#' + arr[i] + '_Complete').attr('src', src);
            $('#' + arr[i] + '_Complete').fadeIn();
            if (hasChild) orareg += 100 / arr.length;
        }
        $('#orareg').html(orareg + '%');
        $('.baro').css("width", orareg + "%");
    }

    pt.showinfo = function (obj) {
        var arr = obj.id.split('_');
        var showid = obj.id.replace(arr[0] + '_', '');
        $('#' + showid).toggle('slow');
    }
    pt.getDateStr = function () {
        var nowdate = new Date();
        var year = nowdate.getFullYear();
        var month = nowdate.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        var day = nowdate.getDate();
        if (day < 10) {
            day = "0" + day;
        }
        var h = nowdate.getHours();
        if (h < 10) {
            h = "0" + h;
        }
        var m = nowdate.getMinutes();
        if (m < 10) {
            m = "0" + m;
        }
        var s = nowdate.getSeconds();
        if (s < 10) {
            s = "0" + s;
        }
        return year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
    }

})();

window.jbs = new jBase();
window._ = jbs._;